#include "StdAfx.h"
#include "DataConvert.h"
#include "CommandDefine.h"


CCommandDefine::CCommandDefine(void)
{
}


CCommandDefine::~CCommandDefine(void)
{
}

int CCommandDefine::GetTotalLength(const ZMVector<unsigned char> &Data)
{
	int len;
	len=GetDataLength(Data)+GetMsgheadLength()+GetDefineTailLength();
	return len;
}

int CCommandDefine::GetMsgheadLength()
{
	return DEVICE_LENGTH_FROM+DEVICE_LENGTH_LENGTH;
}

int CCommandDefine::GetDefineHeadLength()
{
	return DEVICE_HEAD_LENGTH;
}

int CCommandDefine::GetDefineTailLength()
{
	return DEVICE_TAIL_LENGTH;
}

unsigned char CCommandDefine::GetDataFunction(const ZMVector<unsigned char> &Data)
{
	return Data[DEVICE_FUNC_LOCATION];
}

int CCommandDefine::GetDeviceID(const ZMVector<unsigned char> &Data)
{
	return  GetDataWithLength(Data, DEVICE_DEVICE_ID_FROM, DEVICE_DEVICE_ID_LENGTH);
}

int CCommandDefine::GetDeviceType(const ZMVector<unsigned char> &Data)
{
	return  GetDataWithLength(Data, DEVICE_DEVICE_TYPE_FROM, DEVICE_DEVICE_TYPE_LENGTH);
}

void CCommandDefine::SetDataHead(ZMVector<unsigned char> &Data)
{
	int i,j;
	for (i=DEVICE_HEAD_FROM,j=0;j<DEVICE_HEAD_LENGTH;++j)
	{
		Data[i+j]=DEVICE_HEAD_VALUE;
	}
}

void CCommandDefine::SetDataTail(ZMVector<unsigned char> &Data)
{
	int i,j;
	for (i=Data.size()-DEVICE_TAIL_LENGTH,j=0;j<DEVICE_TAIL_LENGTH;++j)
	{
		Data[i+j]=DEVICE_TAIL_VALUE;
	}
}

void CCommandDefine::SetDataFunc(unsigned char cFunc,ZMVector<unsigned char> &Data)
{
	Data[DEVICE_FUNC_LOCATION]=cFunc;
}

int CCommandDefine::SetDataLength(int iLength, ZMVector<unsigned char> &Data)
{
	int iLengthToWrite=iLength;
	if (IsDataType(GetDataFunction(Data))==true)
	{
		iLengthToWrite/=DEVICE_DATA_MULTIY_LENGTH;
	}
	return SetDataWithLength(iLengthToWrite, Data, DEVICE_LENGTH_FROM, DEVICE_LENGTH_LENGTH);
}

int CCommandDefine::GetDataLength(const ZMVector<unsigned char> &Data)
{
	int ret = GetDataWithLength(Data, DEVICE_LENGTH_FROM, DEVICE_LENGTH_LENGTH);
	if (ret > 0)
	{
		if (IsDataType(GetDataFunction(Data)) == true)
		{
			ret *= DEVICE_DATA_MULTIY_LENGTH;
		}
	}
	return ret;
}

bool CCommandDefine::IsDataType(unsigned char Data)
{
	return true;
}

bool CCommandDefine::IsHeadValueTrue(const unsigned char *Data)
{
	int i,j;
	for (i=DEVICE_HEAD_FROM,j=0;j<DEVICE_HEAD_LENGTH;++j)
	{
		if(Data[i+j]!=DEVICE_HEAD_VALUE) return false;
	}
	return true;
}

bool CCommandDefine::IsHeadValueTrue(const ZMVector<unsigned char> &Data)
{
	return IsHeadValueTrue(Data.data());
}

bool CCommandDefine::IsTailValueTrue(const ZMVector<unsigned char> &Data)
{
	int i,j;
	for (i=Data.size()-DEVICE_TAIL_LENGTH,j=0;j<DEVICE_TAIL_LENGTH;++j)
	{
		if(Data[i+j]!=DEVICE_TAIL_VALUE)  return false;
	}
	return true;

}

int CCommandDefine::GetDataWithLength(const ZMVector<unsigned char> &Data, int iDataFrom, int iDataLength)
{
	int ret = -1;
	if (Data.size() >= iDataFrom + iDataLength)
	{
		if (iDataLength == 1)
		{
			ret = Data[iDataFrom];

		}
		else if (2 == iDataLength)
		{
			ret = CDataConvert::Char2ToUInt16(Data[iDataFrom], Data[iDataFrom + 1]);
		}
		else if (4 == iDataLength)
		{
			ret = CDataConvert::Char4ToUInt32(Data[iDataFrom], Data[iDataFrom + 1], Data[iDataFrom + 2], Data[iDataFrom + 3]);
		}
	}
	return ret;
}

int CCommandDefine::SetDataWithLength(int iData, ZMVector<unsigned char> &Data, int iDataFrom, int iDataLength)
{
	int ret = -1;
	if (Data.size() >= iDataFrom + iDataLength)
	{
		if (iDataLength == 1)
		{
			Data[iDataFrom] = iData;

		}
		else if (2 == iDataLength)
		{
			CDataConvert::UInt16ToChar2(iData, Data[iDataFrom], Data[iDataFrom + 1]);
		}

		else if (4 == iDataLength)
		{
			CDataConvert::UInt32ToChar4(
				iData,
				Data[iDataFrom],
				Data[iDataFrom + 1],
				Data[iDataFrom + 2],
				Data[iDataFrom + 3]);
			//ret= CDataConvert::Char4ToUInt32(Data[DEVICE_HEAD_FROM],Data[DEVICE_HEAD_FROM+1],Data[DEVICE_HEAD_FROM+2],Data[DEVICE_HEAD_FROM+3]);
		}
	}
	return ret;
}

int CCommandDefine::SetDeviceID(int iID, ZMVector<unsigned char> &Data)
{
	return  SetDataWithLength(iID, Data, DEVICE_DEVICE_ID_FROM, DEVICE_DEVICE_ID_LENGTH);

}

int CCommandDefine::SetDeviceType(int iType, ZMVector<unsigned char> &Data)
{
	return  SetDataWithLength(iType, Data, DEVICE_DEVICE_TYPE_FROM, DEVICE_DEVICE_TYPE_LENGTH);
}

SharedPointer<ZMVector<unsigned char>> CCommandDefine::InitData(int idDevice, int idDeviceType, unsigned char cFunc, const ZMVector<unsigned char>& data)
{
	SharedPointer<ZMVector<unsigned char>> ret = SharedPointer<ZMVector<unsigned char>>(new ZMVector<unsigned char>(data.size() + CCommandDefine::GetMsgheadLength() + CCommandDefine::GetDefineTailLength()));
	SetDataHead(*ret);
	SetDataTail(*ret);
	SetDeviceID(idDevice, *ret);
	SetDeviceType(idDeviceType, *ret);
	SetDataFunc(cFunc, *ret);
	SetDataLength(data.size(), *ret);

	memcpy( (char*)(ret->data() + GetMsgheadLength()), data.data(), data.size());
	return ret;
}
